package tsinghua.smxq.mc;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import tsinghua.smxq.ui.MCListViewListener;
import tsinghua.smxq.ui.MCMenuAdapter;
import tsinghua.smxq.ui.MCMenuView;
import tsinghua.smxq.weather.Weather;
import tsinghua.smxq.weather.WeatherInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class WeatherSearchActivity extends Activity {
	private TextView todayDate;
	private TextView tomorrowText;
	private TextView thirdDayText;
	private TextView fourthDayText;
	private TextView fifthDayText;
	private TextView currentcity;
	private TextView today_wet;

	private TextView today_temperature;
	private TextView tomorrow_low_temperature;
	private TextView thirdDay_low_temperature;
	private TextView tomorrow_high_temperature;
	private TextView thirdDay_high_temperature;
	private TextView fourthDay_low_temperature;
	private TextView fourthDay_high_temperature;
	private TextView fifthDay_low_temperature;
	private TextView fifthDay_high_temperature;

	private ImageView weatherImg1;
	private ImageView weatherImg2;
	private ImageView weatherImg3;
	private ImageView weatherImg4;
	private ImageView weatherImg5;

	private WeatherInfo wf;
	List<Weather> weatherList;
	String city = "beijing";
	ImageButton setBtn;
	Button menuBtn;
	LayoutInflater inflater;

	public MCMenuView menuView;
	private ListView menuList;
	private View RightView;
	private View[] children;
	private MCMenuAdapter menuAdapter;
	private MCListViewListener menuListener;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		inflater = LayoutInflater.from(this);

		setContentView(inflater.inflate(R.layout.menu_scroll_view, null));

		RightView = inflater.inflate(R.layout.weathershow, null);
		menuView = (MCMenuView) findViewById(R.id.mScrollView);
		menuAdapter = new MCMenuAdapter(this);

		menuList = (ListView) findViewById(R.id.menuList);
		menuListener = new MCListViewListener(this);
		menuList.setAdapter(menuAdapter);
		menuList.setOnItemClickListener(menuListener);
		menuBtn = (Button) RightView.findViewById(R.id.weather_menuBtn);
		menuBtn.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				WeatherSearchActivity.this.menuView.clickMenuBtn();
			}

		});

		wf = new WeatherInfo();
		findView();
		RefreshWeatherData();
		OnClickListener listener = new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("data", "somedata");
				intent.putExtras(bundle);
				intent.setClass(WeatherSearchActivity.this,
						SelectCityActivity.class);
				startActivityForResult(intent, 10);

			}
		};

		currentcity.setOnClickListener(listener);
		setBtn.setOnClickListener(listener);
		View leftView = new View(this);
		children = new View[] { leftView, RightView };
		leftView.setBackgroundColor(Color.TRANSPARENT);
		this.menuView.initViews(children, this.menuBtn, this.menuList);
		this.menuView.setMenuBtn(this.menuBtn);
		MCMenuAdapter.currPosition = 3;

	}

	public void findView() {
		currentcity = (TextView) RightView.findViewById(R.id.currentcity);
		todayDate = (TextView) RightView.findViewById(R.id.todayDate);
		today_wet = (TextView) RightView.findViewById(R.id.today_wet);
		today_temperature = (TextView) RightView
				.findViewById(R.id.today_temperature);
		setBtn = (ImageButton) RightView.findViewById(R.id.set_bn);
		tomorrowText = (TextView) RightView.findViewById(R.id.tomorrowText);
		tomorrow_high_temperature = (TextView) RightView
				.findViewById(R.id.tomorrow_high_temperature);
		tomorrow_low_temperature = (TextView) RightView
				.findViewById(R.id.tomorrow_low_temperature);

		thirdDayText = (TextView) RightView.findViewById(R.id.thirdDayText);
		thirdDay_high_temperature = (TextView) RightView
				.findViewById(R.id.thirdday_high_temperature);
		thirdDay_low_temperature = (TextView) RightView
				.findViewById(R.id.thirdday_low_temperature);

		fourthDayText = (TextView) RightView.findViewById(R.id.fourthdayText);
		fourthDay_high_temperature = (TextView) RightView
				.findViewById(R.id.fourthday_high_temperature);
		fourthDay_low_temperature = (TextView) RightView
				.findViewById(R.id.fourthday_low_temperature);

		fifthDayText = (TextView) RightView.findViewById(R.id.fifthdayText);
		fifthDay_high_temperature = (TextView) RightView
				.findViewById(R.id.fifthday_high_temperature);
		fifthDay_low_temperature = (TextView) RightView
				.findViewById(R.id.fifthday_low_temperature);

		weatherImg1 = (ImageView) RightView.findViewById(R.id.weatherImg1);
		weatherImg2 = (ImageView) RightView.findViewById(R.id.weatherImg2);
		weatherImg3 = (ImageView) RightView.findViewById(R.id.weatherImg3);
		weatherImg4 = (ImageView) RightView.findViewById(R.id.weatherImg4);
		weatherImg5 = (ImageView) RightView.findViewById(R.id.weatherImg5);
	}

	public void RefreshWeatherData() {

		weatherList = wf.setWeatherInfo(city);
		if (weatherList == null) {
			new AlertDialog.Builder(this).setTitle("连接网出错")
					.setMessage("获取天气数据失败，请检查是否开启蜂窝或WiFi网络。")
					.setNegativeButton("确定", null).show();
		} else {
			setDate();
			Weather currentWeather = weatherList.get(0);
			today_temperature.setText(currentWeather.getTemp_c() + "℃");
			today_wet.setText(currentWeather.getHumidity());
			setImg(weatherImg1, currentWeather.getCondition());

			currentWeather = weatherList.get(1);
			tomorrow_low_temperature.setText(currentWeather.getLowTemp() + "℃");
			tomorrow_high_temperature.setText(currentWeather.getHighTemp()
					+ "℃");
			setImg(weatherImg2, currentWeather.getCondition());

			currentWeather = weatherList.get(2);
			thirdDay_low_temperature.setText(currentWeather.getLowTemp() + "℃");
			thirdDay_high_temperature.setText(currentWeather.getHighTemp()
					+ "℃");
			setImg(weatherImg3, currentWeather.getCondition());

			currentWeather = weatherList.get(3);
			fourthDay_low_temperature
					.setText(currentWeather.getLowTemp() + "℃");
			fourthDay_high_temperature.setText(currentWeather.getHighTemp()
					+ "℃");
			setImg(weatherImg4, currentWeather.getCondition());

			currentWeather = weatherList.get(4);
			fifthDay_low_temperature.setText(currentWeather.getLowTemp() + "℃");
			fifthDay_high_temperature.setText(currentWeather.getHighTemp()
					+ "℃");
			setImg(weatherImg5, currentWeather.getCondition());

		}
	}

	public void setDate() {
		Date date = new Date();
		SimpleDateFormat firstDay = new SimpleDateFormat("MM/dd");
		String first = firstDay.format(date);

		todayDate.setText(first + " " + getWeekOfDate(date));
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, 1);
		date = calendar.getTime();

		tomorrowText.setText(getWeekOfDate(date));
		calendar.setTime(date);
		calendar.add(calendar.DATE, 1);
		date = calendar.getTime();

		thirdDayText.setText(getWeekOfDate(date));
		calendar.setTime(date);
		calendar.add(calendar.DATE, 1);
		date = calendar.getTime();

		fourthDayText.setText(getWeekOfDate(date));
		calendar.setTime(date);
		calendar.add(calendar.DATE, 1);
		date = calendar.getTime();

		fifthDayText.setText(getWeekOfDate(date));

		return;

	}

	public void setImg(ImageView imageView, String condition) {
		if (condition.contains("晴")) {
			imageView.setImageResource(R.drawable.qingtian);
		} else if (condition.contains("暴风雨")) {
			imageView.setImageResource(R.drawable.baoyu);
		} else if (condition.contains("雾")) {
			imageView.setImageResource(R.drawable.youwu);
		} else if (condition.contains("雨夹雪")) {
			imageView.setImageResource(R.drawable.yujiaxue);
		} else if (condition.contains("多云")) {
			imageView.setImageResource(R.drawable.duoyun);
		} else if (condition.contains("大雪")) {
			imageView.setImageResource(R.drawable.daxue);
		} else if (condition.contains("雷")) {
			imageView.setImageResource(R.drawable.leiyu);
		} else if (condition.contains("小雪")) {
			imageView.setImageResource(R.drawable.xiaoxue);
		} else if (condition.contains("小雨")) {
			imageView.setImageResource(R.drawable.xiaoyu);
		} else if (condition.contains("中雨")) {
			imageView.setImageResource(R.drawable.zhongyu);
		} else if (condition.contains("中雪")) {
			imageView.setImageResource(R.drawable.zhongxue);
		}
	}

	String getWeekOfDate(Date dt) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}

	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		switch (resultCode) {
		case RESULT_OK:
			Bundle b = intent.getExtras();
			String str = b.getString("Result");
			city = str;
			currentcity.setText(str);
			RefreshWeatherData();
			break;
		case RESULT_CANCELED:

			break;
		default:
			break;
		}
	}

}
